package src.main.java.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class HttpConnectionManager {

	public static String connect(String urlString){
		HttpURLConnection connection = null;
		String retorno = null;
	    try {
	    	
	        URL url = new URL(urlString);
	        connection = (HttpURLConnection) url.openConnection();
	        connection.setRequestProperty("connection", "keep-alive");
	        if(urlString.contains("estoque")){
	        	connection.setRequestProperty("authorization", "Basic ZzQ6ZzQ=");
	        	connection.setRequestMethod("PUT");
	        }
	        if(urlString.contains("mpsolucoesweb")){
	        	connection.setRequestMethod("PUT");
	        }
	        connection.connect();
	        InputStream in = connection.getInputStream();
	        InputStreamReader is = new InputStreamReader(in);
	        StringBuilder sb=new StringBuilder();
	        BufferedReader br = new BufferedReader(is);
	        String read = br.readLine();

	        while(read != null) {
	            sb.append(read);
	            read =br.readLine();
	        }
	        
	        retorno = sb.toString();

	    } catch (MalformedURLException e1) {
	        e1.printStackTrace();
	    } catch (IOException e1) {
	        e1.printStackTrace();
	    } finally {
	        if(null != connection) { connection.disconnect(); }
	    }
	    return retorno;
	}

}
